﻿var app = angular.module("app", ['ngRoute']);
app.config(function ($routeProvider) {
    $routeProvider.when("/", {
        templateUrl: "Views/home.html",
        controller: "mainController"
    }).when("/Category/:category", {
        templateUrl: "Views/home.html",
        controller: "mainController"
    }).when("/Add", {
        templateUrl: "Views/addBeverage.html",
        controller: "addBeverageController"
    }).when("/Search", {
        templateUrl: "Views/list.html",
        controller: "listController"
    }).when("/:id", {
        templateUrl: "Views/home.html",
        controller: "mainController"
    })
        .otherwise({
            templateUrl: "Views/error.html"
        });
});
app.factory("Beverages", function () {
    return [{ "name": "Super", "origin": "USA", "image": "http://www.pakistantribe.com/wp-content/uploads/2014/04/coffee-shops-stockholm.jpg", "category": "TeaCoffee" }, { "name": "Air", "origin": "Brazil", "image": "http://www.catholicvote.org/wp-content/uploads/2013/10/coffee-ending.jpg", "category": "TeaCoffee" }, { "name": "Stewey", "origin": "USA", "image": "http://listdose.com/wp-content/uploads/2013/07/Coffee-Art-by-Michael-Breach.jpg", "category": "TeaCoffee" }, { "name": "Smile", "origin": "Canada", "image": "http://blog.windermerespokane.com/files/2013/11/coffee_smile.jpg", "category": "TeaCoffee" }, { "name": "Heart", "origin": "USA", "image": "http://www.walkaboutflorence.com/sites/default/files/styles/news_detail/public/Coffee_Caffe__Cappuccino_Latte_Florence_Italy.jpg?itok=NLfmSm7j", "category": "TeaCoffee" }];
});
app.factory("Categories", function () {
    return [{ name: "Alcohol", link: "Alcohol" },
            { name: "Tea & Coffee", link: "TeaCoffee" },
            { name: "Misc", link: "Other" }];
});
app.filter("reverse", function () {
    return function (item) {
        item.split("").reverse().join("");
    };
});
app.controller("navController",
    function ($scope, Beverages, Categories) {
        $scope.navs = Categories;
    });
// Rendered in view
app.controller("mainController", function ($scope, $http, Beverages, $routeParams) {
    if ($routeParams.category) {
        $scope.category = $routeParams.category;
    }
    $scope.beverages = Beverages;
    $scope.category = "All Beverages";
    if ($routeParams.category) {
        $scope.category = $routeParams.id;
    }
    else {
    $scope.currentIndex = 0;

    }
    $scope.params = $routeParams;
    $scope.next = function () {
        $scope.currentIndex++;
        if ($scope.currentIndex > Beverages.length - 1) {
            $scope.currentIndex = 0;
        }
        $scope.currentBeverage = Beverages
        [$scope.currentIndex];
    };
    $scope.prev = function () {
        $scope.currentIndex--;
        if ($scope.currentIndex < 0) {
            $scope.currentIndex = Beverages.length - 1;
        }
        $scope.currentBeverage = Beverages
        [$scope.currentIndex];
    };
    $scope.currentBeverage = Beverages[$scope.currentIndex];
    $scope.notify = function () { };
    $http
        .get("https://angu.firebaseio.com/.json")
        .success(
        function (data) {
            while (Beverages.length){
                Beverages.pop();
            }
            for (var x in data) {
                data[x].key = x;
                Beverages.push(data[x]);
            }
            $scope.currentBeverage = Beverages[$scope.currentIndex];
        }
        ).error(
        function () {console.log("Error on Get"); }

        );
});
app.controller("addBeverageController", function ($scope,
    $http, Beverages, Categories, $location) {
    $scope.categories = Categories;
    $scope.saveBeverage = function (beverage) {
        $http
        .post("https://angu.firebaseio.com/.json", beverage)
        .success(
        function (data) {
            beverage.key = data.name;
            var location = (Beverages.push(beverage))-1;
            $location.path("/"+location);
        })
        .error(
        function () {
            console.log("Error");
        });
    };

});
app.controller("editBeverageController", function ($scope,
    $http, Beverages) {
});
app.controller("listController", function ($scope,
    $http, Beverages) {
    $scope.beverages = Beverages;

});